package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.commons.ex.ServiceException;
import cn.tedu.csmall.commons.pojo.vo.PageData;
import cn.tedu.csmall.commons.util.PageInfoToPageDataConverter;
import cn.tedu.csmall.commons.web.ServiceCode;
import cn.tedu.csmall.product.mapper.AttributeMapper;
import cn.tedu.csmall.product.mapper.AttributeTemplateMapper;
import cn.tedu.csmall.product.pojo.entity.Attribute;
import cn.tedu.csmall.product.pojo.param.AttributeAddNewParam;
import cn.tedu.csmall.product.pojo.vo.AttributeListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVo;
import cn.tedu.csmall.product.service.AttributeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AttributeServiceImpl implements AttributeService {

    @Autowired
    AttributeMapper attributeMapper;
    @Autowired
    AttributeTemplateMapper attributeTemplateMapper;

    @Override
    public void addNew(AttributeAddNewParam attributeAddNewParam) {
        log.debug("开始处理【添加属性】的业务，请求参数:{}",attributeAddNewParam);
        QueryWrapper<Attribute> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",attributeAddNewParam.getName());
        int countByName = attributeMapper.selectCount(queryWrapper);
        if (countByName > 0){
            String message = "添加属性失败，属性名称已存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        //检查属性模板是否存在
        Long templateId = attributeAddNewParam.getTemplateId();
        AttributeTemplateStandardVo attributeTemplateStandardVo = attributeTemplateMapper.getStandardById(templateId);
        if (attributeTemplateStandardVo==null){
            String message = "添加属性失败,属性模板数据不存在!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Attribute attribute = new Attribute();
        BeanUtils.copyProperties(attributeAddNewParam,attribute);
        int rows = attributeMapper.insert(attribute);
        if (rows != 1){
            String message = "添加失败，服务器忙，请稍后再试!";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }

    @Override
    public PageData<AttributeListItemVO> listByTemplateId(Long templateId) {
        log.debug("开始处理【根据属性模板ID查询属性列表】的业务，参数：{}",templateId);
        Integer pageNum = 1;
        PageHelper.startPage(pageNum,Integer.MAX_VALUE);
        List<AttributeListItemVO> list = attributeMapper.listByTemplatedId(templateId);
        PageInfo<AttributeListItemVO> pageInfo = new PageInfo<>(list);
        PageData<AttributeListItemVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
